package modules.battle.world
{
	import core.manager.MapManager;
	import core.manager.battle.BattleManager;
	import core.model.PlayerModel;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	
	import framework.proxy.ComponentProxy;
	
	import isoengine.interfaces.IMapObject;
	
	import mapengine.IsoMap;
	import mapengine.avatars.AvatarFactory;
	import mapengine.interfaces.IMapAvatar;
	
	import values.avatar.AvatarTemplateValue;
	import values.avatar.AvatarTemplateValueList;
	import values.avatar.AvatarValue;
	import values.avatar.AvatarValueFactory;
	
	import view.interfaces.IWorldContainer;
	
	public class BattleWorldProxy extends ComponentProxy implements IWorldContainer
	{
		
		[Inject]
		public var playerModel:PlayerModel;
		
		public function BattleWorldProxy()
		{
			super();
		}
		
		public function get displayObject():DisplayObject
		{
			return this.skinContent;
		}
		
		override public function parse(target:MovieClip):void
		{
			MapManager.removeMap();
			
			super.parse(target);
			MapManager.loadMap("map/test.xml", onMapLoaded);
		}
		
		private function onMapLoaded(value:IsoMap):void
		{			
			skinContent.addChildAt(value, 0);
			
			var A:Array = [];
			var avatarV:IMapAvatar;
			for each(var av:AvatarValue in playerModel.myForce.fightAvatars)
			{
				av.initBattleState();
				avatarV = AvatarFactory.createAvatar(av, {x:Math.floor(Math.random() * 3) , y:Math.floor(Math.random() * 20) ,direction:1});
				A.push(avatarV);
				value.addSprite(avatarV);
			}
			
			var B:Array = [];
			
			for each(var v:AvatarTemplateValue in AvatarTemplateValueList.avatarList)
			{
				var avt:AvatarValue = AvatarValueFactory.createAvatarValue(v.key , -1 , null , null) as AvatarValue;
				avt.initBattleState();
				avatarV = AvatarFactory.createAvatar(avt, {x:7 + Math.floor(Math.random() * 3) , y:Math.floor(Math.random() * 20) ,direction:0});
				B.push(avatarV);
				value.addSprite(avatarV);
			}
			
			BattleManager.createBattleManager(A , B , "");
			BattleManager.battleManager.updateRound();	
		}
		
	}
}